package org.iabako.server.serviceimpl.business;

import org.iabako.server.dao.JpaDAO;
import org.iabako.server.dao.business.ClientConversationDAO;
import org.iabako.server.dao.business.QueryDefinitionDAO;
import org.iabako.shared.entity.business.ClientConversation;
import org.iabako.shared.entity.enumeration.TrackingType;
import org.iabako.shared.iservice.business.ClientConversationService;
import org.iabako.shared.iservice.business.TrackingService;
import org.iabako.shared.querydefinition.SearchMotorQueryEnum;
import org.iabako.shared.querydefinition.dto.QueryDefinitionDTO;
import org.iabako.shared.tools.GenericTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;

/**
 * Created by jose on 12/02/15.
 */

@Service("clientConversationService")
public class ClientConversationServiceImpl extends AbstractServiceImpl implements ClientConversationService {

    @Autowired
    ClientConversationDAO clientConversationDAO;
    @Autowired
    private QueryDefinitionDAO queryDefinitionDAO;
    @Autowired
    TrackingService trackingService;

    @Override
    protected JpaDAO getDAO() {
        return clientConversationDAO;
    }

    public QueryDefinitionDTO getSearchQueryDefinitionDTO(QueryDefinitionDTO queryDefinitionDTO) {
        queryDefinitionDTO.setSearchMotorQuery(SearchMotorQueryEnum.ClientConversationSearch);
        queryDefinitionDAO.executeQueryDefinition(queryDefinitionDTO);
        return queryDefinitionDTO;
    }

    public QueryDefinitionDTO setSearchCriteriaFromSuggest(QueryDefinitionDTO qd, Long hiddenSelectionCode, String suggestionValue) {
        return null;
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public ClientConversation save(ClientConversation clientConversation) {

        clientConversation.setDate(GenericTools.getDateFromString(clientConversation.getDateString()));

        if (clientConversation.getId() == null) {
            trackingService.addTrackingToUserSession(TrackingType.conversationNew, clientConversation.getClient());
        } else {
            trackingService.addTrackingToUserSession(TrackingType.conversationModify, clientConversation.getClient());
        }

        return (ClientConversation) super.save(clientConversation);
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Boolean remove(Long id) {
        trackingService.addTrackingToUserSession(TrackingType.conversationDelete, ((ClientConversation) getById(id)).getClient());
        return super.remove(id);
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void addConversationFromEmail(Long idClient, String email, Serializable iabakoMailHelper) {

        //We don't want to pass by hibernate because of useless Client initialization (select queries)
        //It's preferable to pass directly by native queries.
        clientConversationDAO.insertByNativeQuery(idClient, email, iabakoMailHelper);
    }
}
